angular.module('SpecialJudgment')

.factory('SpecialJudgmentHomeService', ['$q', 'Http', 'User', 'Device', 'imanDialog',  
    function($q, Http, User, Device, imanDialog) {

        function getPersonOrMaterielData(code) {

            var defer = $q.defer();

            Http.post({
                url: "/api/Judge/specialScanCode",
                data: {
                    Parameter: {
                        'Barcode': code
                    }
                }
            }).then(function(data) {
                defer.resolve(data);
            }, function(err) {
                defer.reject(err);
            });

            return defer.promise;
        }

        function submitData(Barcode, QCStatus, Remark) {

            var deferred = $q.defer();

            var work = Device.get('Work');
            if (work) {
                Http.post({
                    url: "/api/Judge/doJudge",
                    data: {
                        Parameter: {
                            Barcode: Barcode, // 条码标识
                            TerminalUniqueID: Device.getUUID(), // 设备标识
                            UserID: User.get('_id'), // 用户ID
                            QCStatus: QCStatus,
                            Remark: Remark,
                            Work: work._id
                        }
                    }
                }).then(function(data) {
                    deferred.resolve(data);
                }, function(err) {
                    deferred.reject(err);
                });
            } else {
                imanDialog.toastBottom("当前工位没有绑定作业");
                deferred.reject();
            }

            return deferred.promise;
        }
        return {
            getPersonOrMaterielData: getPersonOrMaterielData,
            submitData: submitData
        };

    }
]);